package com.base.controller.czb.czbapi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.base.controller.api.RedisCacheManager;
import com.base.controller.utils.CZB.CZBOilUtil;
import com.base.controller.utils.CZB.CZBTokenUtils;
import com.base.controller.utils.CZB.LocationUtils;
import com.base.po.param.NetState;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import com.base.service.CZB.GasService;
import com.base.service.SubscribersService;
import com.base.service.UsersService;
import com.base.utils.Produce;
import com.beust.jcommander.internal.Lists;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

@Controller
@RequestMapping("/czbsearch")
public class CZB_SEARCH_REDIS {
    @Autowired
    GasService gasService;
    @Autowired
    UsersService usersService;
    @Autowired
    SubscribersService subscribersService;
    @Autowired
    RedisCacheManager redisCacheManager;

    @CrossOrigin
    @RequestMapping(value = "searchAllOilByRedis", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "api接口", notes = "搜索油站数据")
    public @ResponseBody
    NetState searchOil(HttpServletRequest httpServletRequest, @ApiParam(value = "代理商ID") String parentId, @ApiParam(value = "约定数值：例如OIL_2") String oilNum, @ApiParam(value = "距离，单位米") Double distance, @ApiParam(value = "1距离，2价格") Integer priority,
                       @ApiParam(value = "经度") String longitude, @ApiParam(value = "纬度") String latitude, @ApiParam(value = "手机号") String mobile, @ApiParam(value = "页数") Integer page_size, @ApiParam(value = "从第几页开始") Integer pageNo) {
        Long startTimeBegin = System.currentTimeMillis();
        NetState netState = new NetState();
        Long startTime = System.currentTimeMillis();
        if (page_size == null || page_size.equals(0)) {
            page_size = 10;
        }

        if (pageNo == null) {
            pageNo = 0;
        }

        if (mobile == null || mobile.equals("")) {
            netState.setData("手机信息为空");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        Boolean isMobile = Produce.isMobile(mobile);
        if (!isMobile) {
            netState.setData("手机号码验证不正确");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        if (longitude == null || latitude == null || longitude.equals("") || priority.equals("")) {
            netState.setData("经纬度信息必须上传");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        if (distance == null || distance.equals("")) {
            netState.setData("距离信息必须上传");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        if (oilNum == null || oilNum.equals("")) {
            netState.setData("油耗必须上传");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }

        if (parentId == null || parentId.equals("")) {
            netState.setData("代理商ID必须上传");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }

        String oilnum = CZBOilUtil.findOilNum(oilNum);
        if (oilnum == null) {
            netState.setData("油号信息不正确");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
//        Users users = usersService.selectByPrimaryKey(parentId);
        Boolean haskey = redisCacheManager.hasKey("mobile:" + mobile + "parentid:" + parentId);
        if (!haskey) {
            int userCount = usersService.hasUsers(parentId);
//        List<Subscribers> sub = subscribersService.selectBySub(mobile, parentId);
            int subCount = subscribersService.hasSub(mobile, parentId);
            if (subCount < 1 || userCount < 0) {
                netState.setData("用户信息不正确");
                netState.setErrorCode(NetState.PARAMS_ERROR);
                return netState;
            }
            redisCacheManager.set("mobile:" + mobile + "parentid:" + parentId, "1");
            redisCacheManager.expire("mobile:" + mobile + "parentid:" + parentId, 18000);
        }
//        Boolean flagData=users.getDatastatus();

//        if (!flagData){
//            netState.setData("商户已被冻结，请联系客服");
//            netState.setErrorCode(NetState.PARAMS_ERROR);
//            return netState;
//        }
        Double lat2 = Double.parseDouble(latitude);
        Double lon2 = Double.parseDouble(longitude);

        BigDecimal nowdistance = new BigDecimal(distance);
        String code = LocationUtils.code(longitude, latitude);
        List<Object> gasListString1 = redisCacheManager.lGet("gasListProvince:" + code, 0, -1); //获取所有油站列表
        String gasList1 = gasListString1.get(0).toString();
//        gasList1 = gasList1.substring(0, gasList1.length() - 1);
        String[] gasListString = gasList1.split(",");
        List<Map<Object, Object>> listMapALL = Lists.newArrayList();
        //获取所有的在距离范围内的油站 -----BEGIN------
        for (int i = 0; i < gasListString.length; i++) {
            String gasId = gasListString[i].trim();
            String getCode="GasList:" + code.trim() + "gasID:" + gasId;
            Map<Object, Object> listMap = redisCacheManager.hmget(getCode);
            String lon = listMap.get("longitude").toString();
            String lat = listMap.get("latitude").toString();
            Double lon1 = Double.parseDouble(lon);
            Double lat1 = Double.parseDouble(lat);
            Double distance1 = LocationUtils.GetDistance(lat1, lon1, lat2, lon2);
            Double distancenow = distance - distance1;//是否是超出距离
            String oilPriceList = listMap.get("oilName").toString();
            if (distancenow >= 0) {
                //如果距离为0则判断jedis中的油号是否存在
                if (oilPriceList.contains(CZBOilUtil.findOilNum(oilNum))) {
                    //存在也要把他们的价格存入哦
                    Map<Object, Object> oil1 = redisCacheManager.hmget(getCode+ ":" + CZBOilUtil.findOilNumReal(oilNum));
                    Integer dis = distance1.intValue();
                    listMap.put("distance", dis.toString());
                    listMap.put("id", gasId);
                    listMap.putAll(oil1);
                    listMapALL.add(listMap);
                }
            }
        }
        //对结果进行排序筛选：
        switch (priority) {
            case 1:
                //根据距离排序重新排序
                Collections.sort(listMapALL, new Comparator<Map<Object, Object>>() {
                    public int compare(Map<Object, Object> o1, Map<Object, Object> o2) {
                        Double name1 = Double.parseDouble(o1.get("distance").toString());
                        Double name2 = Double.parseDouble(o2.get("distance").toString());
                        return name1.compareTo(name2);
                    }
                });
                break;
            case 2:
                //根据价格排序重新排序
                Collections.sort(listMapALL, new Comparator<Map<Object, Object>>() {
                    public int compare(Map<Object, Object> o1, Map<Object, Object> o2) {
                        Double name1 = Double.parseDouble(o1.get("priceYfqSearch").toString());
                        Double name2 = Double.parseDouble(o2.get("priceYfqSearch").toString());
                        return name1.compareTo(name2);
                    }
                });
                break;
            default:
                netState.setData("请传入正确的参数");
                netState.setErrorCode(NetState.PARAMS_ERROR);
                return netState;
        }
        //根据距离排序结束，开始根据pageNo进行分页
        Integer pageTotal = 0;
        int start = (pageNo - 1) * 10;
        int end = pageNo * 10;
        if (start >= listMapALL.size()) {
            netState.setData("页数过载");
            netState.setErrorCode(NetState.SUCCESS_CONN);
            return netState;
        }
        List<Map<Object, Object>> result1 = Lists.newArrayList();
        if (listMapALL.size() > 10) {
            if (end > listMapALL.size()) {
                end = listMapALL.size();
            }
            result1 = listMapALL.subList(start, end);
            pageTotal = listMapALL.size() / 10 + 1;
        } else {
            result1 = listMapALL;
            pageTotal = 1;
        }
        //获取分页的list完毕
//        List<Map<String, Object>> map = (List<Map<String, Object>>) result1;
        List<Map<String, Object>> map =Lists.newArrayList();
        for (Map<Object, Object> m1 : result1) {
            Map tempMap = m1;
            Map<String, Object> destMap = tempMap;
            map.add(destMap);
        }
        String gasIds = getGasIds(map);
        Long date = System.currentTimeMillis();
        String token = CZBTokenUtils.getNowToken(mobile);
        String re = CZBTokenUtils.getGasDetails(date, gasIds, mobile, token);
        JSONObject jsonObject1 = JSONObject.parseObject(re);
        JSONArray o1 = jsonObject1.getJSONArray("result");
        Iterator<Map<String, Object>> itor =  map.iterator();//获取key的Iterator便利
       while (itor.hasNext()){
           Map<String, Object> items=itor.next();
               String priceGunSearch = "";
               String priceYfqSearch = "";
               String priceOfficialSearch = "";
               for (Object o : o1) {
                   JSONObject jsonObject11 = JSONObject.parseObject(o.toString());
                   if (items.get("gasid").equals(jsonObject11.getString("gasId"))) {
                       Object ob = jsonObject11.get("oilPriceList");
                       JSONArray jsonArray = JSONArray.parseArray(ob.toString());
                       Boolean flag = false;
                       for (Object o3 : jsonArray) {
                           JSONObject jsonObject = JSONObject.parseObject(o3.toString());
                           String oilNo = jsonObject.getString("oilNo");
                           if (oilNo.equals(CZBOilUtil.findOilNumReal(oilNum))) {
                               priceGunSearch = jsonObject.getString("priceGun");
                               priceYfqSearch = jsonObject.getString("priceYfq");
                               priceOfficialSearch = jsonObject.getString("priceOfficial");
                               flag = true;
                           }
                       }
                       if (flag) {
                           items.put("priceGunSearch", priceGunSearch);
                           items.put("priceYfqSearch", priceYfqSearch);
                           items.put("priceOfficialSearch", priceOfficialSearch);
                           items.put("oilPriceList", ob);
                       } else {
//                           map.remove(items);
                           itor.remove();
                       }
                       break;
                   }
               }
       }
        for (Map<String, Object> items : map)
        //开始对每个List进行拼接
        netState.setData("参数调用成功");
        netState.setErrorCode(NetState.SUCCESS_CONN);
        netState.setJson(result1);
        netState.setOtherJson(pageTotal);
        return netState;
    }

    /**
     * @Date: 2020/4/26 9:17
     * @Description: 新的接口，不再对数据进行处理
     */
    @RequestMapping(value = "searchDataByRedis", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "api接口", notes = "搜索油站数据")
    public @ResponseBody
    NetState searchOil1(HttpServletRequest httpServletRequest, @ApiParam(value = "代理商ID") String parentId, @ApiParam(value = "约定数值：例如OIL_2") String oilNum, @ApiParam(value = "距离，单位米") Double distance, @ApiParam(value = "1距离，2价格") Integer priority,
                        @ApiParam(value = "经度") String longitude, @ApiParam(value = "纬度") String latitude, @ApiParam(value = "手机号") String mobile, @ApiParam(value = "页数") Integer page_size, @ApiParam(value = "从第几页开始") Integer pageNo) {
        NetState netState = new NetState();
//        if (DeviceUtils.isWeChat(httpServletRequest)){
//            netState=searchOilAPI(  parentId, oilNum, distance, priority, longitude, latitude, mobile, page_size, pageNo);
//        }else{
        netState = getAllReal(parentId, oilNum, distance, priority, longitude, latitude, mobile, page_size, pageNo);
//        }

        return netState;

    }

    private NetState getAllReal(String parentId, String oilNum, Double distance, Integer priority, String longitude, String latitude, String mobile, Integer page_size, Integer pageNo) {
        Long startTime = System.currentTimeMillis();
        ;
        NetState netState = new NetState();
        if (page_size == null || page_size.equals(0)) {
            page_size = 10;
        }

        if (pageNo == null) {
            pageNo = 0;
        }

        if (mobile == null || mobile.equals("")) {
            netState.setData("手机信息为空");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        Boolean isMobile = Produce.isMobile(mobile);
        if (!isMobile) {
            netState.setData("手机号码验证不正确");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        if (longitude == null || latitude == null || longitude.equals("") || priority.equals("")) {
            netState.setData("经纬度信息必须上传");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        if (distance == null || distance.equals("")) {
            netState.setData("距离信息必须上传");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        if (oilNum == null || oilNum.equals("")) {
            netState.setData("油耗必须上传");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }

        if (parentId == null || parentId.equals("")) {
            netState.setData("代理商ID必须上传");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }

        String oilnum = CZBOilUtil.findOilNum(oilNum);
        if (oilnum == null) {
            netState.setData("油号信息不正确");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        Users users = usersService.selectByPrimaryKey(parentId);
        List<Subscribers> sub = subscribersService.selectBySub(mobile, parentId);
        if (sub.size() < 1 || users == null) {
            netState.setData("用户信息不正确");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        Boolean flagData = users.getDatastatus();

        if (!flagData) {
            netState.setData("商户已被冻结，请联系客服");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        Double lat2 = Double.parseDouble(latitude);
        Double lon2 = Double.parseDouble(longitude);

        BigDecimal nowdistance = new BigDecimal(distance);
        String gasList1 = redisCacheManager.get("ALLGasId").toString();
        gasList1 = gasList1.substring(1, gasList1.length() - 1);
        String[] gasListString = gasList1.split(",");
        List<Map<Object, Object>> listMapALL = Lists.newArrayList();
        startTime = System.currentTimeMillis();
        ;
        //获取所有的在距离范围内的油站 -----BEGIN------
        for (int i = 0; i < gasListString.length; i++) {
            String gasId = gasListString[i].trim();
            Map<Object, Object> listMap = redisCacheManager.hmget("GasList:" + gasId);
            String lon = listMap.get("longitude").toString();
            String lat = listMap.get("latitude").toString();
            Double lon1 = Double.parseDouble(lon);
            Double lat1 = Double.parseDouble(lat);
            Double distance1 = LocationUtils.GetDistance(lat1, lon1, lat2, lon2);
            Double distancenow = distance - distance1;//是否是超出距离
            String oilPriceList = listMap.get("oilName").toString();
            if (distancenow >= 0) {
                //如果距离为0则判断jedis中的油号是否存在
                if (oilPriceList.contains(CZBOilUtil.findOilNum(oilNum))) {
                    //存在也要把他们的价格存入哦
                    Map<Object, Object> oil1 = redisCacheManager.hmget("GasList:" + gasId + ":" + CZBOilUtil.findOilNumReal(oilNum));
                    Integer dis = distance1.intValue();
                    listMap.put("distance", dis.toString());
                    listMap.put("id", gasId);
                    listMap.putAll(oil1);
                    listMapALL.add(listMap);
                }
            }
        }
        //对结果进行排序筛选：
        switch (priority) {
            case 1:
                //根据距离排序重新排序
                Collections.sort(listMapALL, new Comparator<Map<Object, Object>>() {
                    public int compare(Map<Object, Object> o1, Map<Object, Object> o2) {
                        Double name1 = Double.parseDouble(o1.get("distance").toString());
                        Double name2 = Double.parseDouble(o2.get("distance").toString());
                        return name1.compareTo(name2);
                    }
                });
                break;
            case 2:
                //根据价格排序重新排序
                Collections.sort(listMapALL, new Comparator<Map<Object, Object>>() {
                    public int compare(Map<Object, Object> o1, Map<Object, Object> o2) {
                        Double name1 = Double.parseDouble(o1.get("priceYfqSearch").toString());
                        Double name2 = Double.parseDouble(o2.get("priceYfqSearch").toString());
                        return name1.compareTo(name2);
                    }
                });
                break;
            default:
                netState.setData("请传入正确的参数");
                netState.setErrorCode(NetState.PARAMS_ERROR);
                return netState;
        }
        //根据距离排序结束，开始根据pageNo进行分页
        Integer pageTotal = 0;
        int start = (pageNo - 1) * 10;
        int end = pageNo * 10;
        if (start >= listMapALL.size()) {
            netState.setData("页数过载");
            netState.setErrorCode(NetState.SUCCESS_CONN);
            return netState;
        }
        List<Map<Object, Object>> result1 = Lists.newArrayList();
        if (listMapALL.size() > 10) {
            if (end > listMapALL.size()) {
                end = listMapALL.size();
            }
            result1 = listMapALL.subList(start, end);
            pageTotal = listMapALL.size() / 10 + 1;
        } else {
            result1 = listMapALL;
            pageTotal = 1;
        }
        //获取分页的list完毕
//        List<Map<String, Object>> map = (List<Map<String, Object>>) result1;
        List<Map<String, Object>> map = Lists.newArrayList();
        for (Map<Object, Object> m1 : result1) {
            Map tempMap = m1;
            Map<String, Object> destMap = tempMap;
            map.add(destMap);
        }
        String gasIds = getGasIds(map);
        startTime = System.currentTimeMillis();
        ;
        Long date = System.currentTimeMillis();
        ;
//        String token = CZBTokenUtils.getNowToken(mobile);
//        String re = CZBTokenUtils.getGasDetails(date, gasIds, mobile, token);
//        JSONObject jsonObject1 = JSONObject.parseObject(re);
//        JSONArray o1 = jsonObject1.getJSONArray("result");

        //开始对每个List进行拼接
        netState.setData("参数调用成功");
        netState.setErrorCode(NetState.SUCCESS_CONN);
        netState.setJson(result1);
        netState.setOtherJson(pageTotal);
        return netState;
    }


    public NetState searchOilAPI(String parentId, String oilNum, Double distance, Integer priority,
                                 String longitude, String latitude, String mobile, Integer page_size, Integer pageNo) {
        NetState netState = new NetState();
        if (page_size == null || page_size.equals(0)) {
            page_size = 10;
        }

        if (pageNo == null) {
            pageNo = 0;
        }

        if (mobile == null || mobile.equals("")) {
            netState.setData("手机信息为空");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        Boolean isMobile = Produce.isMobile(mobile);
        if (!isMobile) {
            netState.setData("手机号码验证不正确");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        if (longitude == null || latitude == null || longitude.equals("") || priority.equals("")) {
            netState.setData("经纬度信息必须上传");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        if (distance == null || distance.equals("")) {
            netState.setData("距离信息必须上传");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        if (oilNum == null || oilNum.equals("")) {
            netState.setData("油耗必须上传");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }

        if (parentId == null || parentId.equals("")) {
            netState.setData("代理商ID必须上传");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        String oilnum = CZBOilUtil.findOilNum(oilNum);
        if (oilnum == null) {
            netState.setData("油号信息不正确");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        Users users = usersService.selectByPrimaryKey(parentId);
        List<Subscribers> sub = subscribersService.selectBySub(mobile, parentId);
        if (sub.size() < 1 || users == null) {
            netState.setData("用户信息不正确");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        Double lat2 = Double.parseDouble(latitude);
        Double lon2 = Double.parseDouble(longitude);

        BigDecimal nowdistance = new BigDecimal(distance);

        String gasList1 = redisCacheManager.get("ALLGasId").toString();
        gasList1 = gasList1.substring(1, gasList1.length() - 1);
        String[] gasListString = gasList1.split(",");
        List<Map<Object, Object>> listMapALL = Lists.newArrayList();
        //获取所有的在距离范围内的油站 -----BEGIN------
        for (int i = 0; i < gasListString.length; i++) {
            String gasId = gasListString[i].trim();
            Map<Object, Object> listMap = redisCacheManager.hmget("GasList:" + gasId);
            String lon = listMap.get("longitude").toString();
            String lat = listMap.get("latitude").toString();
            Double lon1 = Double.parseDouble(lon);
            Double lat1 = Double.parseDouble(lat);
            Double distance1 = LocationUtils.GetDistance(lat1, lon1, lat2, lon2);
            Double distancenow = distance - distance1;//是否是超出距离
            String oilPriceList = listMap.get("oilName").toString();
            if (distancenow >= 0) {
                //如果距离为0则判断jedis中的油号是否存在
                if (oilPriceList.contains(CZBOilUtil.findOilNum(oilNum))) {
                    //存在也要把他们的价格存入哦
                    Map<Object, Object> oil1 = redisCacheManager.hmget("GasList:" + gasId + ":" + CZBOilUtil.findOilNumReal(oilNum));
                    Integer dis = distance1.intValue();
                    listMap.put("distance", dis.toString());
                    listMap.put("id", gasId);
                    listMap.putAll(oil1);
                    listMapALL.add(listMap);
                }
            }
        }

        //对结果进行排序筛选：
        switch (priority) {
            case 1:
                //根据距离排序重新排序
                Collections.sort(listMapALL, new Comparator<Map<Object, Object>>() {
                    public int compare(Map<Object, Object> o1, Map<Object, Object> o2) {
                        Double name1 = Double.parseDouble(o1.get("distance").toString());
                        Double name2 = Double.parseDouble(o2.get("distance").toString());
                        return name1.compareTo(name2);
                    }
                });
                break;
            case 2:
                //根据价格排序重新排序
                Collections.sort(listMapALL, new Comparator<Map<Object, Object>>() {
                    public int compare(Map<Object, Object> o1, Map<Object, Object> o2) {
                        Double name1 = Double.parseDouble(o1.get("priceYfqSearch").toString());
                        Double name2 = Double.parseDouble(o2.get("priceYfqSearch").toString());
                        return name1.compareTo(name2);
                    }
                });
                break;
            default:
                netState.setData("请传入正确的参数");
                netState.setErrorCode(NetState.PARAMS_ERROR);
                return netState;
        }
        //根据距离排序结束，开始根据pageNo进行分页
        Integer pageTotal = 0;
        int start = (pageNo - 1) * 10;
        int end = pageNo * 10;
        if (start >= listMapALL.size()) {
            netState.setData("页数过载");
            netState.setErrorCode(NetState.SUCCESS_CONN);
            return netState;
        }
        List<Map<Object, Object>> result1 = Lists.newArrayList();
        if (listMapALL.size() > 10) {
            if (end > listMapALL.size()) {
                end = listMapALL.size();
            }
            result1 = listMapALL.subList(start, end);
            pageTotal = listMapALL.size() / 10 + 1;
        } else {
            result1 = listMapALL;
            pageTotal = 1;
        }
        //获取分页的list完毕
//        List<Map<String, Object>> map = (List<Map<String, Object>>) result1;
        List<Map<String, Object>> map = Lists.newArrayList();
        for (Map<Object, Object> m1 : result1) {
            Map tempMap = m1;
            Map<String, Object> destMap = tempMap;
            map.add(destMap);
        }
        String gasIds = getGasIds(map);

        Long date = System.currentTimeMillis();
        ;
        String token = CZBTokenUtils.getNowTokenApi(mobile);
        String re = CZBTokenUtils.getGasDetailsApi(date, gasIds, mobile, token);
        JSONObject jsonObject1 = JSONObject.parseObject(re);
        JSONArray o1 = jsonObject1.getJSONArray("result");
        map.forEach(items -> {
            String priceGunSearch = "";
            String priceYfqSearch = "";
            String priceOfficialSearch = "";
            for (Object o : o1) {
                JSONObject jsonObject11 = JSONObject.parseObject(o.toString());
                if (items.get("gasid").equals(jsonObject11.getString("gasId"))) {
                    Object ob = jsonObject11.get("oilPriceList");
                    JSONArray jsonArray = JSONArray.parseArray(ob.toString());
                    Boolean flag = false;
                    for (Object o3 : jsonArray) {
                        JSONObject jsonObject = JSONObject.parseObject(o3.toString());
                        String oilNo = jsonObject.getString("oilNo");
                        if (oilNo.equals(CZBOilUtil.findOilNumReal(oilNum))) {
                            priceGunSearch = jsonObject.getString("priceGun");
                            priceYfqSearch = jsonObject.getString("priceYfq");
                            priceOfficialSearch = jsonObject.getString("priceOfficial");
                            flag = true;
                        }
                    }
                    if (flag) {
                        items.put("priceGunSearch", priceGunSearch);
                        items.put("priceYfqSearch", priceYfqSearch);
                        items.put("priceOfficialSearch", priceOfficialSearch);
                        items.put("oilPriceList", ob);
                    } else {
                        map.remove(items);
                    }
                    break;
                }
            }
        });
        //开始对每个List进行拼接
        netState.setData("参数调用成功");
        netState.setErrorCode(NetState.SUCCESS_CONN);
        netState.setJson(result1);
        netState.setOtherJson(pageTotal);
        return netState;
    }

    /**
     * @Date: 2019/11/15 17:02
     * @Description: 生成ids
     */
    public static String getGasIds(List<Map<String, Object>> listMaps) {

        String gasIds = "";
        for (int i = 0; i < listMaps.size(); i++) {
            if (i != 0) {
                gasIds += "," + listMaps.get(i).get("gasid");
            } else {
                gasIds = listMaps.get(i).get("gasid").toString();
            }
        }
        return gasIds;
    }

    @RequestMapping(value = "searchOilPrice", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "API 接口", notes = "搜索油价信息")
    public @ResponseBody
    NetState searchOilPrice(HttpServletRequest httpServletRequest, String parentId, String gasId, String mobile) {
        NetState netState = new NetState();
        Long date = System.currentTimeMillis();
        ;
        String token = CZBTokenUtils.getNowToken(mobile);
        String re = CZBTokenUtils.getGasDetails(date, gasId, mobile, token);
        JSONObject result = JSONObject.parseObject(re);
        JSONObject jsonObject1 = null;
        if (result != null || result.getString("code").equals("200")) {
            jsonObject1 = result.getJSONObject("result");
            netState.setErrorCode(NetState.SUCCESS_CONN);
            netState.setJson(jsonObject1);
            return netState;
        } else {
            netState.setErrorCode(NetState.FEAILED);
            netState.setData("接口调用失败");
            return netState;
        }
    }
}
